package com.auto.survey.activity.monitoring.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.auto.survey.R
import com.auto.survey.activity.monitoring.entity.Point
import com.auto.survey.util.Helper

class CtrlPointAdapter(val data: ArrayList<Point>?)  : Adapter<ViewHolder>() {
    private val mCheckedItems = data ?: arrayListOf() // 默认选中的item
    private var mItems  = mutableListOf<Point>()
    private var mChoiceItems = mutableListOf<Point>()
    private var mListener : OnItemClickListener?=null

    init {
        // 初始化时将默认选中项添加到选择列表
        mChoiceItems.addAll(mCheckedItems)
        Log.i("CtrlPointAdapter", "mChoiceItems size = ${mChoiceItems.size}, mCheckedItems size = ${mCheckedItems.size}")
        mChoiceItems.forEach {
            Log.i("CtrlPointAdapter", "point.name = ${it.pointName}, id = ${it.id}")
        }
    }

    fun setItems(items:List<Point>) {
        mItems.clear() // 清空原有数据

        val point = Point(monitoringObjectId=0,  pointName = "",
            pointX = 0.0, pointY = 0.0, pointZ = 0.0, pointType="")

        mItems.add(0, point)
        mItems.addAll(items)
        notifyDataSetChanged()
    }

    fun setOnItemClickListener(listener:OnItemClickListener) {
        mListener = listener
    }

    override fun getItemViewType(position: Int): Int {
        return if (position == 0) 1 else 2
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

        if (viewType == 1) {
            val view1 = LayoutInflater.from(parent.context).inflate(R.layout.item_create_station_title, parent, false)
            return TitleViewHodler(view1)
        } else {
            val view2 = LayoutInflater.from(parent.context).inflate(R.layout.item_view_create_station_ctrl_point, parent, false)
            return MyViewHodler(view2)
        }
    }

    override fun getItemCount(): Int {
        if (mItems.isNotEmpty()) {
            return mItems.size
        }
        return 0
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val type = getItemViewType(position)

        if (type == 2) {
            val myhodler = holder as MyViewHodler
            val item = mItems[position]

            myhodler.tv_cs_point_name.text = item.pointName
            myhodler.tv_cs_point_x.text = Helper.formateDecimal(item.pointX).toString()
            myhodler.tv_cs_point_y.text = Helper.formateDecimal(item.pointY).toString()
            myhodler.tv_cs_point_z.text = Helper.formateDecimal(item.pointZ).toString()

            // 检查当前项是否应该被选中
            Log.i("CtrlPointAdapter", "isItemChecked = ${isItemChecked(item)}")
            myhodler.bx_cs_point.isChecked = isItemChecked(item)

            myhodler.ll_cs_item_root.setOnClickListener {
                myhodler.bx_cs_point.isChecked = !myhodler.bx_cs_point.isChecked

                if(myhodler.bx_cs_point.isChecked){
                    mChoiceItems.add(mItems[position])
                } else {
                    mChoiceItems.remove(mItems[position])
                }

                Log.i("CtrlPointAdapter", "items size = ${mChoiceItems.size}")


                mListener?.onItemClick(mChoiceItems)
            }
        }

    }

    // 判断项是否应该被选中
    private fun isItemChecked(item: Point): Boolean {

        Log.i("CtrlPointAdapter", "item.id = ${item.id}， item.name = ${item.pointName}")

//        mChoiceItems.forEach {
//            Log.i("CtrlPointAdapter", "it.id = ${it.id}, item.id = ${item.id}, item.name = ${item.pointName}")
//        }
        return mChoiceItems.any { it.id == item.id }
    }


    private class TitleViewHodler(itemview:View): RecyclerView.ViewHolder(itemview){}

    private class MyViewHodler(itemview:View): RecyclerView.ViewHolder(itemview){
        val ll_cs_item_root = itemview.findViewById<LinearLayout>(R.id.ll_cs_item_root)
        val bx_cs_point = itemview.findViewById<CheckBox>(R.id.bx_cs_point_name)
        val tv_cs_point_name = itemview.findViewById<TextView>(R.id.tv_cs_point_name)
        val tv_cs_point_x = itemview.findViewById<TextView>(R.id.tv_cs_point_x)
        val tv_cs_point_y = itemview.findViewById<TextView>(R.id.tv_cs_point_y)
        val tv_cs_point_z = itemview.findViewById<TextView>(R.id.tv_cs_point_z)
    }

    interface OnItemClickListener {
        fun onItemClick(items:List<Point>)
    }
}